import csv

import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np


class Notebook:
    def __init__(self, Name, PID, NotebookID, Position, Price, CPU_Name, CPU_Frequency, CPU_MAX_Frequency, CPU_CT,
                 CPU_L3Cache, CPU_CoreName, CPU_Process, CPU_Power, RAM_Capacity, RAM_Type, ROM_Capacity, ROM_Type,
                 Display_Touch, Display_Type, Display_Size, Display_Scale, Display_Resolution, Display_Brightness,
                 Display_Ratio, Display_Technology, Display_RefreshRate, Display_Gamut, GPU_Name, GPU_Capacity,
                 GPU_Type, GPU_BW, GPU_MAX_Power, GPU_Dedicated, Loudspeaker, Microphone, Wireless, Wired_Network,
                 Bluetooth, Data_Interface, Video_Interface, Audio_Interface, Other_Interface, Battery_Type,
                 Power_Adapter, Weight, Length, Width, Thickness, Material, Color):
        self.Name = Name
        self.PID = PID
        self.NotebookID = NotebookID
        self.Position = Position
        self.Price = Price
        self.CPU_Name = CPU_Name
        self.CPU_Frequency = CPU_Frequency
        self.CPU_MAX_Frequency = CPU_MAX_Frequency
        self.CPU_CT = CPU_CT
        self.CPU_L3Cache = CPU_L3Cache
        self.CPU_CoreName = CPU_CoreName
        self.CPU_Process = CPU_Process
        self.CPU_Power = CPU_Power
        self.RAM_Capacity = RAM_Capacity
        self.RAM_Type = RAM_Type
        self.ROM_Capacity = ROM_Capacity
        self.ROM_Type = ROM_Type
        self.Display_Touch = Display_Touch
        self.Display_Type = Display_Type
        self.Display_Size = Display_Size
        self.Display_Scale = Display_Scale
        self.Display_Resolution = Display_Resolution
        self.Display_Brightness = Display_Brightness
        self.Display_Ratio = Display_Ratio
        self.Display_Technology = Display_Technology
        self.Display_RefreshRate = Display_RefreshRate
        self.Display_Gamut = Display_Gamut
        self.GPU_Name = GPU_Name
        self.GPU_Capacity = GPU_Capacity
        self.GPU_Type = GPU_Type
        self.GPU_BW = GPU_BW
        self.GPU_MAX_Power = GPU_MAX_Power
        self.GPU_Dedicated = GPU_Dedicated
        self.Loudspeaker = Loudspeaker
        self.Microphone = Microphone
        self.Wireless = Wireless
        self.Wired_Network = Wired_Network
        self.Bluetooth = Bluetooth
        self.Data_Interface = Data_Interface
        self.Video_Interface = Video_Interface
        self.Audio_Interface = Audio_Interface
        self.Other_Interface = Other_Interface
        self.Battery_Type = Battery_Type
        self.Power_Adapter = Power_Adapter
        self.Weight = Weight
        self.Length = Length
        self.Width = Width
        self.Thickness = Thickness
        self.Material = Material
        self.Color = Color

    def tranTo(self, begin, after):
        if begin == '产品定位':
            self.Position = after
        elif begin == 'CPU型号':
            self.CPU_Name = after
        elif begin == 'CPU主频':
            self.CPU_Frequency = after
        elif begin == '最高睿频':
            self.CPU_MAX_Frequency = after
        elif begin == '核心':
            self.CPU_CT = after
        elif begin == '三级缓存':
            self.CPU_L3Cache = after
        elif begin == '核心代号':
            self.CPU_CoreName = after
        elif begin == '制程工艺':
            self.CPU_Process = after
        elif begin == '功耗':
            self.CPU_Power = after
        elif begin == '内存容量':
            self.RAM_Capacity = after
        elif begin == '内存类型':
            self.RAM_Type = after
        elif begin == '硬盘容量':
            self.ROM_Capacity = after
        elif begin == '硬盘描述':
            self.ROM_Type = after
        elif begin == '触控屏':
            self.Display_Touch = after
        elif begin == '屏幕类型':
            self.Display_Type = after
        elif begin == '屏幕尺寸':
            self.Display_Size = after
        elif begin == '显示比例':
            self.Display_Scale = after
        elif begin == '屏幕分辨率':
            self.Display_Resolution = after
        elif begin == '亮度':
            self.Display_Brightness = after
        elif begin == '屏占比':
            self.Display_Ratio = after
        elif begin == '屏幕技术':
            self.Display_Technology = after
        elif begin == '屏幕刷新率':
            self.Display_RefreshRate = after
        elif begin == '色域':
            self.Display_Gamut = after
        elif begin == '显卡芯片':
            self.GPU_Name = after
        elif begin == '显存容量':
            self.GPU_Capacity = after
        elif begin == '显存类型':
            self.GPU_Type = after
        elif begin == '显存位宽':
            self.GPU_BW = after
        elif begin == '显卡最高功耗':
            self.GPU_MAX_Power = after
        elif begin == '独显直连':
            self.GPU_Dedicated = after
        elif begin == '扬声器':
            self.Loudspeaker = after
        elif begin == '麦克风':
            self.Microphone = after
        elif begin == '无线网卡':
            self.Wireless = after
        elif begin == '有线网卡':
            self.Wired_Network = after
        elif begin == '蓝牙':
            self.Bluetooth = after
        elif begin == '数据接口':
            self.Data_Interface = after
        elif begin == '视频接口':
            self.Video_Interface = after
        elif begin == '音频接口':
            self.Audio_Interface = after
        elif begin == '其它接口':
            self.Other_Interface = after
        elif begin == '电池类型':
            self.Battery_Type = after
        elif begin == '电源适配器':
            self.Power_Adapter = after
        elif begin == '笔记本重量':
            self.Weight = after
        elif begin == '长度':
            self.Length = after
        elif begin == '宽度':
            self.Width = after
        elif begin == '厚度':
            self.Thickness = after
        elif begin == '外壳材质':
            self.Material = after
        elif begin == '外壳描述':
            self.Color = after
        elif begin == 'None':
            self.Color = after


def get_html(url):
    # 模拟浏览器访问
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54',
        'accept-language': 'zh-CN,zh;q=0.9'
    }
    print("--> 正在获取网站信息")
    response = requests.get(url, headers=headers)  # 请求访问网站
    if response.status_code == 200:
        html = response.text  # 获取网页源码
        return html  # 返回网页源码
    else:
        print("获取网站信息失败！")


if __name__ == '__main__':
    csv_file = open('NotebookInfo.csv', 'w', newline='', encoding='utf-8')
    writer = csv.writer(csv_file)
    writer.writerow(
        ['Name', 'PID', 'NotebookID', 'Position', 'Price', 'CPU_Name', 'CPU_Frequency', 'CPU_MAX_Frequency', 'CPU_CT',
         'CPU_L3Cache', 'CPU_CoreName', 'CPU_Process', 'CPU_Power', 'RAM_Capacity', 'RAM_Type', 'ROM_Capacity',
         'ROM_Type', 'Display_Touch', 'Display_Type', 'Display_Size', 'Display_Scale', 'Display_Resolution',
         'Display_Brightness', 'Display_Ratio', 'Display_Technology', 'Display_RefreshRate', 'Display_Gamut',
         'GPU_Name', 'GPU_Capacity', 'GPU_Type', 'GPU_BW', 'GPU_MAX_Power', 'GPU_Dedicated', 'Loudspeaker',
         'Microphone', 'Wireless', 'Wired_Network', 'Bluetooth', 'Data_Interface', 'Video_Interface', 'Audio_Interface',
         'Other_Interface', 'Battery_Type', 'Power_Adapter', 'Weight', 'Length', 'Width', 'Thickness', 'Material',
         'Color'])

    data = pd.read_csv("../IDs/Notebook.csv")
    col_1 = data["ID"]
    col_2 = data['PicPath']
    col_3 = data['Name']
    col_4 = data['Price']
    ids = np.array(col_1)
    picPath = np.array(col_2)
    names = np.array(col_3)
    prices = np.array(col_4)
    for i in range(len(ids)):
    # for i in range(10):
        search_url = 'https://detail.zol.com.cn/1428/' + str(ids[i]) + '/param.shtml'
        html = get_html(search_url)
        soup = BeautifulSoup(html, 'lxml')
        beginList = soup.find_all('th')
        afterList = soup.find_all('td', class_='hover-edit-param')
        case = Notebook('null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null',
                        'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null',
                        'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null',
                        'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null',
                        'null', 'null')
        case.NotebookID = ids[i]
        case.Name = names[i]
        case.PID = picPath[i]
        case.Price = prices[i]
        for j in range(len(beginList)):
            try:
                begin = beginList[j].span.get_text()
                after = afterList[j].span.get_text()
            except:
                begin = beginList[j].a.get_text()
                after = afterList[j].span.get_text()
            case.tranTo(begin, after)
        writer.writerow(
            [case.Name, case.PID, case.NotebookID, case.Position, case.Price, case.CPU_Name, case.CPU_Frequency,
             case.CPU_MAX_Frequency, case.CPU_CT, case.CPU_L3Cache, case.CPU_CoreName, case.CPU_Process, case.CPU_Power,
             case.RAM_Capacity, case.RAM_Type, case.ROM_Capacity, case.ROM_Type, case.Display_Touch, case.Display_Type,
             case.Display_Size, case.Display_Scale, case.Display_Resolution, case.Display_Brightness,
             case.Display_Ratio, case.Display_Technology, case.Display_RefreshRate, case.Display_Gamut, case.GPU_Name,
             case.GPU_Capacity, case.GPU_Type, case.GPU_BW, case.GPU_MAX_Power, case.GPU_Dedicated, case.Loudspeaker,
             case.Microphone, case.Wireless, case.Wired_Network, case.Bluetooth, case.Data_Interface,
             case.Video_Interface, case.Audio_Interface, case.Other_Interface, case.Battery_Type, case.Power_Adapter,
             case.Weight, case.Length, case.Width, case.Thickness, case.Material, case.Color])
        i += 1
    csv_file.close()
